﻿@model Xms.Web.Models.UsersInRoleModel

<div class="row">
    <div class="col-sm-2" id="myScrollspy">
        <div class="list-group" id="myNav">
            <a href="#section-2" class="list-group-item" data-target="#section-2">@app.T["role"] <em class="pull-right glyphicon glyphicon glyphicon-chevron-right"></em></a>
            <a href="#section-1" class="active list-group-item" data-target="#section-1">@app.T["user"] <em class="pull-right glyphicon glyphicon glyphicon-chevron-right"></em></a>
        </div>
    </div>
    <div class="col-sm-10" id="scroll-body">
        <div id="section-1" class="panel panel-default">
            <div class="panel-heading">
                <div class="panel-title">
                    <strong>@app.PageTitle</strong>
                    <div class="pull-right">
                        <a class="btn btn-info btn-xs" href="javascript:addUsers()"><span class="glyphicon glyphicon-user"></span> 加入用户</a>
                        <a class="btn btn-warning btn-xs" href="javascript:removeUsers()">
                            <span class="glyphicon glyphicon-minus-sign"></span> 移除用户
                        </a>
                    </div>
                </div>
            </div>
            <div class="table-responsive" id="gridview">
                <table class="table table-striped table-hover table-condensed" id="datatable" data-ajax="true" data-ajaxcontainer="gridview" data-ajaxcallback="ajaxgrid_reset()" data-sortby="@Model.SortBy.ToLower()" data-sortdirection="@Model.SortDirection">
                    <thead>
                        <tr>
                            <th width="2%"><input type="checkbox" name="checkall" /></th>
                            <th>
                                @app.T["name"]
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Items)
                        {
                            <tr>
                                <td><input type="checkbox" value="@item.SystemUserId" name="recordid" /></td>
                                <td>@item.UserName</td>
                            </tr>
                        }
                    </tbody>
                </table>
                <div class="panel-footer">
                    <div class="row">
                        <div class="col-sm-4">
                            @(app.T["pagination_label"].Replace("{page}", Model.Page.ToString()).Replace("{totalpages}", Model.TotalPages.ToString()).Replace("{totalitems}", Model.TotalItems.ToString()))
                        </div>
                        <div id="page-selection" class="col-sm-8" data-total="@Model.TotalPages" data-page="@Model.Page"></div>
                    </div>
                </div>
                <script>
            var pageUrl = '@app.Url';
                </script>
            </div>
        </div>
        <div id="section-2" class="hide" data-url="@("/"+app.OrganizationUniqueName)/role/editrole?id=@Model.RoleId">
        </div>
    </div>
</div>

@section Header {
    <link href="/content/js/bootstrap-datepicker-1.5.0/css/bootstrap-datepicker3.min.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
}
@section Scripts {
    <script src="/content/js/jquery.bootpag.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
        //var pageUrl = $("#datatable").attr('data-pageurl');
        var roleid = '@Model.RoleId';
        $(function () {
            $("#datatable").ajaxTable();
            ajaxgrid_reset();
            $("#myNav").affix({
                offset: {
                    top: 125
                }
            });
            $('#myNav').on('click', 'a', function (e) {
                var target = $($(this).attr('data-target'));
                var dataUrl = target.attr('data-url');
                $(this).siblings("a").removeClass("active").end().addClass("active");
                if (dataUrl) {
                    location.href = dataUrl;
                }
                $('#scroll-body > div').addClass('hide');
                target.removeClass('hide');
            });
        });
        function ajaxgrid_reset() {
            pag_init();
            Xms.Web.DataTable($("#datatable"));
        }
        function pag_init() {
            $('#page-selection').bootpag({
                total: $('#page-selection').attr('data-total')
                , maxVisible: 10
                , page: $('#page-selection').attr('data-page')
                , leaps: false
                , prev: '&lsaquo;'
                , next: '&rsaquo;'
                , firstLastUse: true
                , first: '&laquo;'
                , last: '&raquo;'
            }).on("page", function (event, /* page number here */ num) {
                event.preventDefault();
                var url = $.setUrlParam(pageUrl, 'page', num);
                $("#gridview").ajaxLoad(url, "#gridview", function (response) {
                    //$("#gridview").replaceWith($(this)); // some ajax content loading...
                    ajaxgrid_reset();
                });
                return false;
            });
        }
        function addUsers() {
            Xms.Web.OpenDialog('/entity/RecordsDialog?entityname=systemuser&singlemode=false', 'selectRecordCallback')
        }
        function selectRecordCallback(result, inputid) {
            var ids = [];
            $(result).each(function (i, n) {
                ids.push(n.id);
            });
            Xms.Web.Post('/security/AddUserRoles', { userid: ids, roleid: [roleid] }, false, function (response) {
                if (response.IsSuccess) {
                    Xms.Web.Toast(response.IsSuccess, response.Content);
                    location.reload(true);
                }
                else {
                    Xms.Web.Alert(false, response.Content);
                }
            });
        }
        function removeUsers() {
            Xms.Web.Confirm('', '', function () {
                var target = $('#datatable');
                var ids = Xms.Web.GetTableSelected(target);
                if (!ids || ids.length == 0) {
                    Xms.Web.Toast(LOC_NOTSPECIFIED_RECORD, false);
                    return;
                }
                Xms.Web.Post('/security/RemoveUserRoles', { userid: ids, roleid: roleid }, false, function (response) {
                    if (response.IsSuccess) {
                        Xms.Web.Toast(response.IsSuccess, response.Content);
                        location.reload(true);
                    }
                    else {
                        Xms.Web.Alert(false, response.Content);
                    }
                });
            });
        }
    </script>
}